from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict


def jwt_response_payload_handler(token, user=None, request=None):
    # token,             认证成功之后,系统生成的token
    # user=None,         认证成功之后的user信息
    # request=None       登录的请求

    return {
        'token': token,
        'username': user.username,
        'user_id': user.id
    }

# def jwt_response_payload_handler(token, user=None, request=None):
#     return {
#         'token': token,
#         'username': user.username,
#         'user_id': user.id
#     }


class PageNum(PageNumberPagination):
    page_size = 5
    page_size_query_param = 'pagesize'
    max_page_size = 50

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),  # 总共记录条数
            ('lists', data),  # 结果类别
            ('page', self.page.number),  # 当前页码
            ('pages', self.page.paginator.num_pages),  # 总页数
            ('pagesize', self.page_size),  # 一页多少条记录
        ]))


# class PageNum(PageNumberPagination):
#     page_size = 5
#     page_size_query_param = 'pagesize'
#     max_page_size = 50
#
#     def get_paginated_response(self, data):
#         return Response(OrderedDict([
#             ('count', self.page.paginator.count),  # 总共记录条数
#             ('lists', data),  # 结果类别
#             ('page', self.page.number),  # 当前页码
#             ('pages', self.page.paginator.num_pages),  # 总页数
#             ('pagesize', self.page_size),  # 一页多少条记录
#         ]))
